<?php

/**
 * 房源拍卖信息
 */

namespace app\api\model;

use PDOStatement;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;
use think\Db;

class HouseAuction extends Model {

    /**
     * 根据House查询历史拍卖
     * @param $house_id
     * @return array
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */

    //拍卖阶段
    const TYPE_1 = 1;
    const TYPE_2 = 2;
    const TYPE_3 = 3;
    const TYPE_4 = 4;
    const TYPE_5 = 5;
    const TYPE_6 = 6;

    const TYPE_CONFIG = [
        self::TYPE_1 => '一拍流拍',
        self::TYPE_2 => '二拍流拍',
        self::TYPE_3 => '三拍流拍',
        self::TYPE_4 => '四拍流拍',
        self::TYPE_5 => '五拍流拍',
        self::TYPE_6 => '六拍流拍',
    ];

    const TYPE_CONFIG_ = [
        self::TYPE_1 => '一拍',
        self::TYPE_2 => '二拍',
        self::TYPE_3 => '三拍',
        self::TYPE_4 => '变卖',
        self::TYPE_5 => '重新拍卖',
        self::TYPE_6 => '破产',
    ];

    //拍卖状态
    const A_STATUS1 = 1;
    const A_STATUS2 = 2;
    const A_STATUS3 = 3;
    const A_STATUS4 = 4;
    const A_STATUS5 = 5;
    const A_STATUS6 = 6;
    const A_STATUS7 = 7;
    public static $a_status = [
        self::A_STATUS1 => '待开始',
        self::A_STATUS2 => '进行中',
        self::A_STATUS3 => '已成交',
        self::A_STATUS4 => '已流拍',
        self::A_STATUS5 => '已撤回',
        self::A_STATUS6 => '已中止',
        self::A_STATUS7 => '已暂缓',
    ];

    //装修类型
    const RENOVA_TYPE0 = 0;
    const RENOVA_TYPE1 = 1;
    const RENOVA_TYPE2 = 2;
    const RENOVA_TYPE3 = 3;
    const RENOVA_TYPE4 = 4;
    const RENOVA_CONFIG = [
        self::RENOVA_TYPE0 => '未知',
        self::RENOVA_TYPE1 => '清水',
        self::RENOVA_TYPE2 => '简装',
        self::RENOVA_TYPE3 => '中装',
        self::RENOVA_TYPE4 => '精装',
    ];

    //成交
    const SUCCESS_1 = 1;
    const SUCCESS_2 = 2;
    const SUCCESS_3 = 3;
    const SUCCESS_4 = 4;
    const SUCCESS_5 = 5;
    const SUCCESS_CONFIG = [
        self::TYPE_1 => '一拍成交',
        self::TYPE_2 => '二拍成交',
        self::TYPE_3 => '三拍成交',
        self::TYPE_4 => '变卖成交',
        self::TYPE_5 => '重新拍卖成交',
    ];

    /**
     * 历史拍卖
     * @param $house_id
     * @return array|PDOStatement|string|Collection
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function getHistory($house_id, $type) {
        $data = Db::name('house_auction')
            ->where('house_id', $house_id)
            ->where('type', '<>', $type)
            ->field('type,start_time,end_time,IF(type=4,pay_selloff,initial_price) as initial_price')
            ->order('type', 'asc')
            ->select();
        if ($data) {
            foreach ($data as $k => $v) {
                $data[$k]['typename'] = self::TYPE_CONFIG[$v['type']];

                deal_money_list($data[$k]['initial_price'], $initial_price, $initial_price_unit);
                $data[$k]['initial_price'] = $initial_price;
                $data[$k]['initial_price_unit'] = $initial_price_unit;

                $data[$k]['start_time'] = !empty($data[$k]['start_time']) ? date('Y年m月d日', $v['start_time']) : '';
                $data[$k]['end_time'] = !empty($data[$k]['end_time']) ? date('Y年m月d日', $v['end_time']) : '';

                unset($data[$k]['type']);
            }
        } else {
            $data = [];
        }
        return $data;
    }

}
